package com.yc.other.mail;
import java.io.File;
import java.io.UnsupportedEncodingException;
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;


import javax.activation.*;
 
public class SendEmail2
{
   public void send()
   {   
	   String to = "1050689875@qq.com";
	   String from = "18906514196@189.cn";
//	   String from = "yunduanyou@126.com";
	   String userName = from;
//	   String password = "1234qwer";
	   String password = "Sheng678";
	   

      String host = "smtp.189.cn";
      
      //发件箱是否是exchange
      boolean isExchange = false;
      String domain = "360ser.com";
      
      //是否有附件
      boolean haveFile = false;
      String filename = "G:\\temp\\file\\pom.xml";
//      String filename = filepath;
      File sendFile = new File(filename);
 
      // 获取系统属性
      Properties properties = System.getProperties();
 
      // 设置邮件服务器
      properties.setProperty("mail.transport.protocol", "SMTP");
      properties.setProperty("mail.smtp.host", host);
      properties.setProperty("mail.smtp.port", "25");
 
      // 指定验证为true
      properties.setProperty("mail.smtp.auth", "true");
      properties.setProperty("mail.smtp.timeout","1000");
      
      if(isExchange){
          if(domain == null || domain.equals("")){
              throw new RuntimeException("domain is null");
          }
          properties.setProperty("mail.smtp.auth.ntlm.domain", domain);
      }
      // 验证账号及密码，密码需要是第三方授权码
      MailAuthenticator smtpAuth = new MailAuthenticator(userName, password);
      Session session = Session.getInstance(properties, smtpAuth);
      session.setDebug(true);
      try{
         // 创建默认的 MimeMessage 对象
         MimeMessage message = new MimeMessage(session);
 
         // Set From: 头部头字段
         message.setFrom(new InternetAddress(from));
 
         // Set To: 头部头字段
         message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));
         message.addRecipient(Message.RecipientType.CC, new InternetAddress(userName));
 
         // Set Subject: 主题
         message.setSubject("测试文件2432421313213");
         
         
         try {
        	 
        	 MimeMultipart mp = new MimeMultipart();//message中的的根节点
        	 MimeBodyPart mbp_mix1 = new MimeBodyPart();//混合体节点1（包含正文和其他内嵌文档资源）
             MimeBodyPart mbp_attach1 = new MimeBodyPart();//附件节点1
             //添加混合体
             MimeMultipart mbp_mix_mmp = new MimeMultipart();//混合体节点内的根节点
             
             MimeBodyPart mbp_mix_mmp_mbp_text = new MimeBodyPart();//正文
             mbp_mix_mmp_mbp_text.setContent("刘总，你好！1232142143214213", "text/html;charset=gbk");
             
             mbp_mix_mmp.addBodyPart(mbp_mix_mmp_mbp_text);
             mbp_mix1.setContent(mbp_mix_mmp);
             mp.addBodyPart(mbp_mix1);
             //添加附件
             if (haveFile) {
            	 FileDataSource fds = new FileDataSource(filename);
            	 mbp_attach1.setDataHandler(new DataHandler(fds));
            	 mbp_attach1.setFileName(MimeUtility.encodeText(sendFile.getName()));
            	 mp.addBodyPart(mbp_attach1);
             }
             
             message.setContent(mp); // Multipart加入到信件
             message.setSentDate(new Date()); // 设置信件头的发送日期
         } catch(Exception e) {
        	 e.printStackTrace();
         }
         
         // 发送消息
         Transport.send(message);
         System.out.println("Sent message successfully....");
      }catch (MessagingException mex) {
         mex.printStackTrace();
      }
   }
   
   public static void main(String[] args) {
	   SendEmail2 se = new SendEmail2();
	   se.send();
   }
}
